<template>
    <div id="ComplaintIndex">
      <group label-width="6.5em">
        <popup-picker placeholder="请选择投诉类型" title="投 诉 类 型" :data="list" v-model="value"  @on-change="onChange" value-text-align="bottom"></popup-picker>
      </group>
      <group label-width="6.5em">
        <x-input class="in" title="投诉订单号" disabled placeholder="选择投诉单号" @click.native="linkNum" v-model="num"></x-input>
      </group>
      <group class="wa-sel">
        <cell title="投诉原由" >
        </cell>
        <x-textarea :max="200" name="description" v-model="text" placeholder="说明投诉详细理由" :onKeypress="checkFun()"></x-textarea>
      </group>
      <x-button type="primary" style="margin: 30px auto 0" action-type="button" @click.native="listSubmit">提交</x-button>
      <div v-transfer-dom>
        <alert v-model="showAlert" @on-hide="hideFun"> {{ contentAlert }}</alert>
      </div>
    </div>
</template>

<script>
    import {  Cell , Group , XInput, PopupPicker,XTextarea,XButton,Alert,AlertModule,TransferDomDirective as TransferDom,cookie } from 'vux'
    import { sorts,sign } from '../../aes/make';
    import { Decrypt} from '../../aes/aes';
    import { creditComplaintInformationAdd } from '../../mock/api';
    export default {
      name: "complaint-index",
      directives: {
        TransferDom
      },
      components: {
        Group,
        Cell,
        XInput,
        PopupPicker,
        XTextarea,
        XButton,
        Alert
      },
      data(){
        return{
          size:'',
          num:'',
          value: ['订单投诉'],
          list: [['订单投诉', '未按时间发货', '拒绝正常售后','超时未取旧电池','平台投诉','其他']],
          listJson:{
            1:'订单投诉',
            2:'其他',
            3:'未按时间发货',
            4:'拒绝正常售后',
            5:'超时未取旧电池',
            6:'平台投诉'
          },
          text:'',
          showAlert:false,
          contentAlert:'',
          code:0,
        }
      },
      computed:{

      },
      created () {
        let vue = this
        vue.updateTitle('投诉');
        vue.updateGreenRightNum(0);
        vue.updateUrlBack('/index.php?s=/shop/user/personal_center');
        vue.updatePreventGoBack(true);
        this.value[0]=this.$route.query.complaintType==undefined?'订单投诉':this.$route.query.complaintType;
        var order_sn=this.$route.query.orderNum;
        if(this.$route.query.refuse==1){
          this.value=['拒绝正常售后']
        }
        if(order_sn!='' || order_sn!=undefined){
          this.num=order_sn;
        }
      },
      mounted(){

      },
      methods: {
        checkFun(){
          if(/[" '/~`<>%)(&*+]/.test(this.text)){
            this.text=this.text.replace(/[" '/~`<>%;)(&*+]/,"")
          }
        },
        linkNum(){
          if(this.value[0]=='超时未取旧电池' || this.value[0]=='平台投诉' || this.value[0]=='其他'){
              return false;
          }else if(this.value[0]=='拒绝正常售后'){
            this.$router.push({ name: 'complaintList', query: {complaintType:this.value[0], orderList:1 }})
          }else{
            this.$router.push({ name: 'complaintList', query: {complaintType:this.value[0], listType: 'shop',orderList:1 }})
          }
        },
        onChange(val){
          if(val=='超时未取旧电池' || val=='平台投诉' || val=='其他'){
            this.num='不可选择';
          }
        },
        listSubmit(){
          var order_sn=this.$route.query.orderNum;
          for(var j in this.listJson){
            if(this.value[0]==this.listJson[j]){
              var complaint_type=j;
            }
          }
          var complaint_text=this.text;
          var isCust=0
          if(this.$route.query.listType=='service'){
            isCust=1;
          }
          if(this.value[0]=='订单投诉' || this.value[0]=='未按时间发货' || this.value[0]=='拒绝正常售后'){
              if(order_sn=='' || order_sn==undefined){
                this.showAlert=true;
                this.contentAlert='请选择订单号';
                return false;
              }
          }else{
            this.showAlert=true;
            this.contentAlert='暂不能投诉';
            return false;
          }
          var so=sorts();
          var uid=cookie.get('uid');
          var access_token=cookie.get('access_token');
          var json={
            order_sn:order_sn,
            complaint_type:complaint_type,
            complaint_text:complaint_text,
            isCust:isCust,
            sorts:so,
            uid:uid,
            access_token:access_token
          }
          var si=sign(json);
          json['sign']=si;
          creditComplaintInformationAdd(json).then((res)=>{
            if(res.code==40013){
              vue.$router.push({name: 'login', params: {}});
              return false;
            }
            this.showAlert=true;
            this.contentAlert=res.message;
            this.code=res.code;
          })
        },
        hideFun(){
          if(this.code==2000){
            window.location.href='/index.php?s=/shop/user/personal_center';
          }
        }
      }
    }
</script>

<style lang="less" scoped>
</style>
